<?php
/**
* Change logs
* Feb 22, 2011 - trungdt - Write function show for homepage schedule show
* Feb 23, 2011 - trungdt - Rewrite pray regis list, write view Show Pray Registration
*/
class praymodule {
    function name() { return "Pray Module"; }
    function description() { return "Praymodule, for PrayOnline"; }
    function author() { return 'ABSoft'; }

    function hasContent() { return true; }
    function hasSources() { return true; }
    function hasViews()   { return true; }

    function supportsWorkflow() { return false; }

    function permissions($internal = '') {
        return array(
        "view_subjects"=>"Xem chủ đề",
        "view_ring"=>"Xem các vòng cầu nguyện",
        "view_pastor"=>"Xem DS các mục sư",
        "approve_user"=>"Duyệt đăng ký thành viên",

        );
    }

    function deleteIn($loc) {
        // Do nothing, no content
    }

    function copyContent($from_loc,$to_loc) {
        // Do nothing, no content
    }

    function getLocationHierarchy($loc) {
        // because this module does not belong to any page, we fake source to get permission
        if ($loc->src=="")
        {
            $loc->src="@praymodule"; 
        }
        if ($loc->int == '') return array($loc);
        else return array($loc,exponent_core_makelocation($loc->mod,$loc->src));
    }

    function show($view,$loc = null, $title = '') {
        global $db, $user;

        /** THIS VALUE MUST BE IMPLEMENT BETTER 
        $max_day = 2; // max_day = 3: today and next 2 days
        // first, we build the default structure for schedule 
        $schedule=array();
        $day=time();
        for ($i=0;$i<$max_day;$i++){
        $schedule_day=null;
        $schedule_day->title=_ab_lang_dateTitle($day);
        $schedule_day->date=$day;
        $schedule_day->hours=array();
        for ($k=0;$k<24;$k++){
        $schedule_hour=array();
        for ($j=0;$j<4;$j++){
        $schedule_temp=null;
        // this flag tell smarty that this time does not have any one register
        $schedule_temp->person_count=0;
        $schedule_hour[$j]=clone $schedule_temp;
        }
        $schedule_day->hours[$k]=$schedule_hour;
        }
        $schedule[$i]=$schedule_day;
        $day=strtotime("+1 day",$day);
        }
        // then, we query on DB, and put the pray registration on 
        // the right position

        // get pray rings
        $rings=$db->selectObjects("pray_ring");   **/

        // Feb 23, 2011 - trungdt - comment out function pray regis table, 
        //                          rewrite pray regis list
        $regis=array();
        if (exponent_users_isLoggedIn())
        {
            $regis=$db->selectObjectsBySql("
            SELECT {$db->prefix}pray_register.*
            FROM {$db->prefix}pray_register 
            WHERE {$db->prefix}pray_register.user_id = '{$user->id}' 
            ORDER BY fhour, fmin, thour, tmin
            ");
            if (count($regis)>0)
            {
                foreach ($regis as $key=>$value)
                {
                    // convert to user local time
                    $value->fhour_local=0;
                    $value->thour_local=0;
                    $value->fmin_local=0;
                    $value->tmin_local=0;
                    aboid_localtime_convert($value->fhour,$value->fmin, $value->fhour_local, $value->fmin_local);
                    aboid_localtime_convert($value->thour,$value->tmin, $value->thour_local, $value->tmin_local);
                    // pretty time make
                    $value->fhour=str_pad($value->fhour,2,"0",STR_PAD_LEFT);
                    $value->fmin=str_pad($value->fmin,2,"0",STR_PAD_LEFT);
                    $value->thour=str_pad($value->thour,2,"0",STR_PAD_LEFT);
                    $value->tmin=str_pad($value->tmin,2,"0",STR_PAD_LEFT);
                    // 
                    $value->fhour_local=str_pad($value->fhour_local,2,"0",STR_PAD_LEFT);
                    $value->fmin_local=str_pad($value->fmin_local,2,"0",STR_PAD_LEFT);
                    $value->thour_local=str_pad($value->thour_local,2,"0",STR_PAD_LEFT);
                    $value->tmin_local=str_pad($value->tmin_local,2,"0",STR_PAD_LEFT);
                }
            }   
            // Feb 23, 2011 - trungdt  - end                                    

            // Mar 9, 2011 - - trungdt - Get rings registed
            $rings=$db->selectObjectsBySql("
            SELECT {$db->prefix}pray_register_ring.*, {$db->prefix}pray_ring.name
            FROM {$db->prefix}pray_register_ring 
            INNER JOIN {$db->prefix}pray_ring 
            ON {$db->prefix}pray_register_ring.ring_id = {$db->prefix}pray_ring.id
            WHERE {$db->prefix}pray_register_ring.user_id = {$user->id}
            ");
            // end of Mar 9

            // parse template
            $template = new template('praymodule',$view,$loc);
            //$template->assign("schedules",$schedule);
            if (exponent_users_isLoggedIn())
            {
                $template->assign("logged",1);    
            }
            else
            {
                $template->assign("logged",0);    
            }
            //$template->assign("rings",$rings);
            // Feb 23, - begin
            $time_belt=7;

            if (exponent_users_isLoggedIn())
                $time_belt=$user->time_belt;
            $template->assign("pray_regis",$regis);  
            $template->assign("time_belt",$time_belt);
            $template->assign("rings",$rings);
            $template->assign("count_rings",count($rings));
            $template->assign("server_belt","+7");
            // Feb 23, - end
            $template->output($view);
        }
    }

    function spiderContent($item = null) {
        // Do nothing, no content
        return false;
    }

}